//#include "stdafx.h"
#include "sqldb_utility.hpp"

namespace sqldb {//////////////

#ifdef	__SQLDB_ENABLE_MYSQL
extern shared_ptr<DataBase> __create_mysql_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_ORACLE
extern shared_ptr<DataBase> __create_oracle_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_SQLITE
extern shared_ptr<DataBase> __create_sqlite_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_PGSQL
extern shared_ptr<DataBase> __create_pgsql_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_CARBONDATA
extern shared_ptr<DataBase> __create_carbondata_db_inst();
#endif


shared_ptr<DataBase> DataBase::CreateInstance(const string& db_type)
{
#ifdef	__SQLDB_ENABLE_MYSQL
	if(db_type.compare("mysql")==0)
		return __create_mysql_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_ORACLE
	if(db_type.compare("oracle")==0) 
		return __create_oracle_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_SQLITE
	if(db_type.compare("sqlite")==0)
		return __create_sqlite_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_PGSQL
	if (db_type.compare("pgsql") == 0)
		return __create_pgsql_db_inst();
#endif
#ifdef	__SQLDB_ENABLE_CARBONDATA
	if (db_type.compare("carbondata") == 0)
		return __create_carbondata_db_inst();
#endif
    shared_ptr<DataBase> dataBase;
    return dataBase;
}



shared_ptr<DataBase> SqlDb_InitConn(const string& dbtype, const string& conn_url, const string& uname, const string& pswd, const string& init_paras)
{
	shared_ptr<DataBase> sdb = DataBase::CreateInstance(dbtype);
	sdb->connect(conn_url,uname,pswd);
	//parse ini_param
	string initparm(init_paras);
	for(char* ps=(char*)initparm.data();*ps!=0;) {
		char* pe=strchr(ps,'|');
		if(pe) {
			*pe=0;
			sdb->exec_no_query(ps);
			ps=pe+1;
		}else {//find to the string end
			sdb->exec_no_query(ps);
			break;
		}
	}
	return sdb;
}

//=======================================================
DbBindPara make_bp_null() {
	return DbBindPara(DB_TYPE_NULL,0,0);
}

DbBindPara make_bp(const char* str) {
	return DbBindPara(DB_TYPE_STRING,str,strlen(str));
}

DbBindPara make_bp(const string& str) {
	return DbBindPara(DB_TYPE_STRING,&str,(size_t)-1);
}

DbBindPara make_bp(int val) {
	DbBindPara dp(DB_TYPE_INT,0,0);
	dp.i_val=val;
	return dp;
}

DbBindPara make_bp(long long val){
	DbBindPara dp(DB_TYPE_INT64,0,0);
	dp.i64_val=val;
	return dp;
}

DbBindPara make_bp(float val){
	DbBindPara dp(DB_TYPE_FLOAT,0,0);
	dp.f_val=val;
	return dp;
}

DbBindPara make_bp(double val){
	DbBindPara dp(DB_TYPE_DOUBLE,0,0);
	dp.d_val=val;
	return dp;
}



}///////////////////////////////////
